<!-- 采购清单分页组件 -->
<template>
  <div class="pagination u-flex-center">
    <div class="pagination_wrap u-flex u-row-center">
      <!-- 上一页按钮 -->
      <div
        class="page_btn u-flex-center hand"
        @click="prevPage"
        :class="{ disabled: currentPage === 1 }"
      >
        <img src="../../image/mine/arrow.png" alt="上一页" />
      </div>

      <!-- 页码按钮 -->
      <div
        class="page_num u-flex-center hand"
        v-for="page in visiblePages"
        :key="page"
        :class="{ active: page == currentPage }"
        @click="goToPage(page)"
      >
        {{ page }}
      </div>

      <!-- 省略号 -->
      <!-- <div class="page_ellipsis" v-if="showStartEllipsis">...</div> -->

      <!-- 第一页 -->
      <!-- <div class="page_num u-flex-center hand" v-if="showStartEllipsis" @click="goToPage(1)">1</div> -->

      <!-- 省略号 -->
      <div class="page_ellipsis" v-if="showEndEllipsis">...</div>

      <!-- 最后一页 -->
      <div class="page_num u-flex-center hand" v-if="showEndEllipsis" @click="goToPage(totalPages)">
        {{ totalPages }}
      </div>

      <!-- 下一页按钮 -->
      <div
        class="page_btn u-flex-center hand"
        @click="nextPage"
        :class="{ disabled: currentPage === totalPages }"
      >
        <img src="../../image/mine/arrow.png" alt="下一页" style="transform: rotate(180deg)" />
      </div>
    </div>

    <!-- 页面信息 -->
    <div class="page_info u-flex u-row-center">
      <span>第 {{ currentPage }} 页，共 {{ totalPages }} 页</span>
      <span class="page_jump">
        跳转至第
        <input
          type="number"
          class="jump_input"
          v-model="jumpPage"
          @keyup.enter="jumpToPage"
          min="1"
          :max="totalPages"
        />
        页
      </span>
    </div>
  </div>
</template>

<script setup>
  import { computed, ref, watch } from 'vue'

  // Props
  const props = defineProps({
    // 当前页码
    currentPage: {
      type: Number,
      default: 1,
    },
    // 每页显示数量
    pageSize: {
      type: Number,
      default: 6,
    },
    // 总数据量
    total: {
      type: Number,
      default: 0,
    },
    // 最大显示的页码数量
    maxVisible: {
      type: Number,
      default: 5,
    },
  })

  // Emits
  const emit = defineEmits(['update:currentPage', 'page-change'])

  // 响应式数据
  const jumpPage = ref(1)

  // 计算属性
  const totalPages = computed(() => {
    return Math.ceil(props.total / props.pageSize)
  })

  const visiblePages = computed(() => {
    const pages = []
    const maxVisible = props.maxVisible
    let start = Math.max(1, props.currentPage - Math.floor(maxVisible / 2))
    let end = Math.min(totalPages.value, start + maxVisible - 1)

    if (end - start + 1 < maxVisible) {
      start = Math.max(1, end - maxVisible + 1)
    }

    for (let i = start; i <= end; i++) {
      pages.push(i)
    }

    return pages
  })

  const showStartEllipsis = computed(() => {
    return visiblePages.value[0] > 2
  })

  const showEndEllipsis = computed(() => {
    return visiblePages.value[visiblePages.value.length - 1] < totalPages.value - 1
  })

  // 监听当前页码变化，更新跳转页码
  watch(
    () => props.currentPage,
    newPage => {
      jumpPage.value = newPage
    },
    { immediate: true }
  )

  // 方法
  const prevPage = () => {
    if (props.currentPage > 1) {
      const newPage = props.currentPage - 1
      emit('update:currentPage', newPage)
      emit('page-change', newPage)
    }
  }

  const nextPage = () => {
    if (props.currentPage < totalPages.value) {
      const newPage = props.currentPage + 1
      emit('update:currentPage', newPage)
      emit('page-change', newPage)
    }
  }

  const goToPage = page => {
    if (page >= 1 && page <= totalPages.value && page !== props.currentPage) {
      emit('update:currentPage', page)
      emit('page-change', page)
    }
  }

  const jumpToPage = () => {
    const page = parseInt(jumpPage.value)
    if (page >= 1 && page <= totalPages.value) {
      emit('update:currentPage', page)
      emit('page-change', page)
    } else {
      jumpPage.value = props.currentPage
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
    margin-top: 30px;
    gap: 20px;

    .pagination_wrap {
      gap: 8px;

      .page_btn {
        width: 32px;
        height: 32px;
        border-radius: 4px;
        background: #fff;
        border: 1px solid #d9d9d9;
        cursor: pointer;
        transition: all 0.3s;

        img {
          width: 5px;
          height: auto;
        }

        &:hover:not(.disabled) {
          border-color: #5463ff;
          color: #5463ff;
        }

        &.disabled {
          opacity: 0.5;
          cursor: not-allowed;
        }
      }

      .page_num {
        width: 32px;
        height: 32px;
        border-radius: 4px;
        background: #fff;
        border: 1px solid #d9d9d9;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          border-color: #5463ff;
          color: #5463ff;
        }

        &.active {
          background: #5463ff;
          border-color: #5463ff;
          color: #fff;
        }
      }

      .page_ellipsis {
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #999;
      }
    }

    .page_info {
      gap: 20px;
      font-size: 14px;
      color: #666;

      .page_jump {
        gap: 8px;

        .jump_input {
          width: 40px;
          height: 32px;
          border: 1px solid #d9d9d9;
          border-radius: 4px;
          text-align: center;
          font-size: 14px;
          outline: none;

          &:focus {
            border-color: #5463ff;
          }

          /* 移除所有浏览器的上下箭头 */
          &[type='number'] {
            -moz-appearance: textfield; /* Firefox */
            appearance: textfield; /* 其他现代浏览器 */
          }

          /* 针对 WebKit 浏览器（Chrome, Safari, Edge） */
          &[type='number']::-webkit-outer-spin-button,
          &[type='number']::-webkit-inner-spin-button {
            -webkit-appearance: none;
            margin: 0;
          }
        }
      }
    }
  }

  // 响应式适配
  @media screen and (max-width: 1920px) {
    .pagination {
      margin-top: 25px !important;
      gap: 12px !important;

      .pagination_wrap {
        gap: 6px !important;

        .page_btn {
          width: 28px !important;
          height: 28px !important;

          img {
            width: 4px !important;
          }
        }

        .page_num {
          width: 28px !important;
          height: 28px !important;
          font-size: 12px !important;
        }

        .page_ellipsis {
          width: 28px !important;
          height: 28px !important;
          font-size: 12px !important;
        }
      }

      .page_info {
        gap: 15px !important;
        font-size: 12px !important;

        .page_jump {
          gap: 4px !important;

          .jump_input {
            width: 35px !important;
            height: 28px !important;
            font-size: 12px !important;
          }
        }
      }
    }
  }

  @media screen and (max-width: 1700px) {
    .pagination {
      margin-top: 20px !important;
      gap: 10px !important;

      .pagination_wrap {
        gap: 5px !important;

        .page_btn {
          width: 24px !important;
          height: 24px !important;

          img {
            width: 3px !important;
          }
        }

        .page_num {
          width: 24px !important;
          height: 24px !important;
          font-size: 10px !important;
        }

        .page_ellipsis {
          width: 24px !important;
          height: 24px !important;
          font-size: 10px !important;
        }
      }

      .page_info {
        gap: 12px !important;
        font-size: 10px !important;

        .page_jump {
          gap: 3px !important;

          .jump_input {
            width: 30px !important;
            height: 24px !important;
            font-size: 10px !important;
          }
        }
      }
    }
  }

  @media screen and (max-width: 1500px) {
    .pagination {
      margin-top: 18px !important;
      gap: 8px !important;

      .pagination_wrap {
        gap: 4px !important;

        .page_btn {
          width: 22px !important;
          height: 22px !important;

          img {
            width: 3px !important;
          }
        }

        .page_num {
          width: 22px !important;
          height: 22px !important;
          font-size: 9px !important;
        }

        .page_ellipsis {
          width: 22px !important;
          height: 22px !important;
          font-size: 9px !important;
        }
      }

      .page_info {
        gap: 10px !important;
        font-size: 9px !important;

        .page_jump {
          gap: 2px !important;

          .jump_input {
            width: 28px !important;
            height: 22px !important;
            font-size: 9px !important;
          }
        }
      }
    }
  }
</style>
